package Sudoku;

import java.util.Scanner;

public class sud {
	static Scanner leer = new Scanner(System.in);
	static int mat[][] = new int[9][9];

	public static void main(String[] args) {
		int opc;
		llenado();

		do {
			System.out.println(">>>> ELIGA OPCION <<<<");
			System.out.println("1. MOSTRAR");
			System.out.println("2. INSERTAR");
			System.out.println("3. SALIR");

			opc = leer.nextInt();
			if (opc != 1 && opc != 2 && opc != 3)
				System.out.println("OPCION INVALIDA SOLO OPCION 1.2 O 3");
			switch (opc) {// sw
			case 1:
				mostrar();
				break;
			case 2:
				insertar();
				break;
			case 3:
				System.out.println(">>>> ADIOS <<<<");
				break;
			} // sw
		} while (opc != 3);
	} // clase

	static void llenado() {
		mat[0][0] = 5;
		mat[0][4] = 8;
		mat[0][5] = 6;
		mat[0][8] = 1;
		mat[1][2] = 2;
		mat[1][3] = 7;
		mat[1][5] = 1;
		mat[1][6] = 6;
		mat[2][1] = 7;
		mat[2][2] = 1;
		mat[2][6] = 2;
		mat[2][7] = 5;
		mat[3][0] = 9;
		mat[3][1] = 1;
		mat[3][4] = 2;
		mat[3][7] = 7;
		mat[4][0] = 3;
		mat[4][3] = 1;
		mat[4][4] = 4;
		mat[4][5] = 5;
		mat[4][8] = 6;
		mat[5][1] = 6;
		mat[5][4] = 9;
		mat[5][7] = 2;
		mat[5][8] = 4;
		mat[6][1] = 5;
		mat[6][2] = 3;
		mat[6][6] = 4;
		mat[6][7] = 6;
		mat[7][2] = 8;
		mat[7][3] = 9;
		mat[7][5] = 3;
		mat[7][6] = 5;
		mat[8][0] = 2;
		mat[8][3] = 5;
		mat[8][4] = 1;
		mat[8][8] = 7;

	}

	static void mostrar() {
		int f, c;

		for (f = 0; f < 9; f++) {

			for (c = 0; c < 9; c++) {
				if (c == 0 && f == 0)
					System.out.println("f/c|c1|c2|c3|/|c4|c5|c6|/|c7|c8|c9|");

				if (c == 0)
					System.out.print("f" + (f + 1) + " ");

				System.out.print("| " + mat[f][c]);

				if (c == 2 || c == 5)
					System.out.print("|/");

			}
			System.out.println("|");
			if (f == 2 || f == 5)
				System.out.println("   ---------|/|--------|/|---------");
		}

	}

	static void insertar() {

		int f, c, nf, nc, dato, sw = 0, exi = 0;

		do {
			System.out.println(">>>> ingrese ubicacion <<<<");
			System.out.print(">>>> fila :");
			nf = leer.nextInt() - 1;
			System.out.print(">>>> colomna :");
			nc = leer.nextInt() - 1;
			if (nf < 1 && nf > 9 && nc < 1 && nc > 9)
				System.out.println("solo numeros de 1-9 ");

			if (nf == 0 && nc == 0 || nf == 0 && nc == 4 || nf == 0 && nc == 5
					|| nf == 0 && nc == 8 || nf == 1 && nc == 2 || nf == 1
					&& nc == 3 || nf == 1 && nc == 5 || nf == 1 && nc == 6
					|| nf == 2 && nc == 1 || nf == 2 && nc == 2 || nf == 2
					&& nc == 6 || nf == 2 && nc == 7 || nf == 3 && nc == 0
					|| nf == 3 && nc == 1 || nf == 3 && nc == 4 || nf == 3
					&& nc == 7 || nf == 4 && nc == 0 || nf == 4 && nc == 3
					|| nf == 4 && nc == 4 || nf == 4 && nc == 5 || nf == 4
					&& nc == 8 || nf == 5 && nc == 1 || nf == 5 && nc == 4
					|| nf == 5 && nc == 7 || nf == 5 && nc == 8 || nf == 6
					&& nc == 1 || nf == 6 && nc == 2 || nf == 6 && nc == 6
					|| nf == 6 && nc == 7 || nf == 7 && nc == 2 || nf == 7
					&& nc == 3 || nf == 7 && nc == 5 || nf == 7 && nc == 6
					|| nf == 8 && nc == 0 || nf == 8 && nc == 3 || nf == 8
					&& nc == 4 || nf == 8 && nc == 8) {

				System.out
				.println("pocision bloqueada,el numero es parte del cuadro ");
				insertar();
				return;
			}

		} while (nf < 1 && nf > 9 && nc < 1 && nc > 9);

		if (mat[nf][nc] != 0) {
			do {
				System.out.print("ubicacion ocupada reemplazar 1.si/0.no");
				sw = leer.nextInt();
			} while (sw != 0 && sw != 1);
		} else
			sw = 1;

		if (sw == 1) {
			String mensaje=new String();
			System.out.print("ingrese dato :");
			dato = leer.nextInt();

			for (f = 0; f < 9; f++) {
				if (mat[f][nc] == dato) {
					exi = 1;
					System.out.println("dato ya existe en f:" + (f + 1) + " c:"
							+ (nc + 1));
				}
			}
			for (c = 0; c < 9; c++) {
				if (mat[nf][c] == dato) {
					exi = 1;
					System.out.println(" dato ya esxiste en f:" + (nf + 1)
							+ " c:" + (c + 1));
				}
			}

			if (exi == 0)
				mat[nf][nc] = dato;

		}
		mostrar();

	}

}
